package tsvetanv.inventory.model;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;

/**
 * Article class presents an article in a stock. It includes a {@link}Product
 * and its quantity.
 * 
 * @author Tsvetan Vasilev
 * 
 */

@Entity
public class Article implements Serializable {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int id;

	@OneToOne(cascade = CascadeType.ALL)
	private Product product;

	@Column(precision = 12, scale = 2)
	private double quantity;

	// TODO equals(), toString() methods overriding + UnitTests

	// --- Constructors, Getters/Setters ---------------------------------------

	public Article() {
		// Initialize Product because the DataBinder is not able to set
		// properties on nested objects that are null.
		product = new Product();
	}

	public Article(int id, Product product, Double quantity) {
		this.id = id;
		this.product = product;
		this.quantity = quantity;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public Double getQuantity() {
		return quantity;
	}

	public void setQuantity(Double quantity) {
		this.quantity = quantity;
	}

	// --- Methods -------------------------------------------------------------
	@Override
	public String toString() {
		StringBuilder strBuilder = new StringBuilder(50);
		strBuilder.append("Article: id= ").append(getId());
		strBuilder.append(", quantity= ").append(getQuantity());
		strBuilder.append(", ").append(getProduct().toString());
		return strBuilder.toString();
	}
}
